<template>
  <div class="app-container">
    <div class="filter-container">

      <el-form :model="listQuery" ref="filterForm">
        <el-row>
          <el-col :span="4">
            <el-form-item label-width="100px" label="二维码批次号" class="postInfo-container-item">
              <el-input v-model="listQuery.batch_num" autocomplete="off" clearable placeholder="批次号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label-width="100px" label="UUID" class="postInfo-container-item">
              <el-input v-model="listQuery.uuid" autocomplete="off" clearable placeholder="UUID"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="7" :offset="1">
            <el-form-item label-width="80px" label="时间范围:">
              <el-date-picker
                v-model="daterange"
                type="daterange"
                value-format="yyyy-MM-dd"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :picker-options="pickerOptions"
                @change="daterangeHandle">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
              查询
            </el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div style="margin-top: 10px">
      <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
        <el-tab-pane label="全部" name="-1"></el-tab-pane>
        <el-tab-pane label="未领取" name="0"></el-tab-pane>
        <el-tab-pane label="已领取" name="1"></el-tab-pane>
      </el-tabs>
    </div>

    <el-table ref="dragTable" v-loading="listLoading" :data="list" row-key="id" fit highlight-current-row
              style="width: 100%;margin-top:30px;">
      <el-table-column align="center" label="二维码">
        <template slot-scope="scope">
          {{ scope.row.qrcode }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="金额">
        <template slot-scope="scope">
          {{ scope.row.money }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="状态">
        <template slot-scope="scope">
          {{statusShow[scope.row.status]}}
        </template>
      </el-table-column>
<!--      <el-table-column align="center" label="有效期">-->
<!--        <template slot-scope="scope">-->
<!--          {{ scope.row.begin_time}}至 {{ scope.row.end_time}}-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column align="center" label="领取人">
        <template slot-scope="scope">
          {{ scope.row.user_nickname}}
        </template>
      </el-table-column>
      <el-table-column align="center" label="手机号">
        <template slot-scope="scope">
          {{ scope.row.mobile}}
        </template>
      </el-table-column>
      <el-table-column align="center" label="领取地点">
        <template slot-scope="scope">
          {{ scope.row.address}}
        </template>
      </el-table-column>
      <el-table-column align="center" label="领取时间">
        <template slot-scope="scope">
          {{ scope.row.used_time}}
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleView(scope)" v-if="scope.row.status!==1">
            修改金额
          </el-button>

        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
                @pagination="getList"/>
    <el-dialog :visible.sync="dialogVisible" title="修改金额">
      <el-form :model="data" label-position="left" label-width="120px">
        <el-form-item label="选择金额" required>
          <el-radio-group v-model="data.spec_id" >
            <el-radio :label="item.id"  v-for="item in data.specs" >￥{{item.money}}</el-radio>
            <el-radio :label="0"><el-input v-model="data.diyMoney" placeholder="自定义金额" /></el-radio>
          </el-radio-group>
        </el-form-item>

      </el-form>
      <div style="text-align:right;">
        <el-button type="danger" @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="saveData">保存</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import waves from '@/directive/waves' // waves directive

  import Pagination from '@/components/Pagination'
  import { addRedPacket, getRedPacket, getRedPacketUrl } from '../../api/redPacket'
  import { getRedPacketDetail, getRedPacketDetails, updateRedPacketDetail } from '../../api/redPacketDetail'

  const statusShow = {
    0: '未领取',
    1: '已领取',
  }
  export default {
    components: { Pagination },
    directives: { waves },

    filters: {
      statusFilter(status) {
        const statusMap = {
          2: 'info',
          1: 'success'
        }
        return statusMap[status] ?? 'danger'
      },
      statusShowFilter(status) {
        return statusShow[status] ?? '无'
      }
    },
    data() {
      return {
        pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          }]
        },
        statusShow,
        activeName: '-1',
        specs: [],
        daterange: [],
        data: {},
        list: [],
        dialogVisible: false,
        dialogType: 'new',
        total: 0,
        listLoading: true,
        listQuery: {
          page: 1,
          limit: 20
        }
      }
    },
    created() {
      this.getList()
    },
    methods: {

      handleClick(tab, event){
        if (tab.name=='-1')
        {
          delete  this.listQuery.status
        }else
        {
          this.listQuery.status=tab.name
        }
        this.getList()
      },
      saveData() {
        const param ={
          money:this.data.diyMoney,
          spec_id:this.data.spec_id,
        }
        updateRedPacketDetail(this.data.id,param).then(res => {
          this.$message.success('修改成功！')
          this.getList()
          this.dialogVisible = false
        })
      },
      handleAdd() {
        this.data = {}
        this.dialogVisible = true
        this.dialogType = 'new'
        this.specs = []
      },
      daterangeHandle(value) {
        if (value)
        {
          this.listQuery.begin_time = value[0]
          this.listQuery.end_time = value[1]
        }else {
         delete this.listQuery.begin_time
          delete this.listQuery.end_time
        }
      },
      handleFilter() {
        this.listQuery.page = 1
        this.getList()
      },
      async getList() {
        this.listLoading = true
        const res = await getRedPacketDetails(this.listQuery)
        this.list = res.data.data
        this.total = res.data.total
        this.listLoading = false
      },
      handleView(scope) {
        getRedPacketDetail(scope.row.id).then(res=>{
          this.data =res.data
          this.data.id =scope.row.id
        })
        this.dialogVisible = true
        this.dialogType = 'edit'
        this.specs = this.data.specs
      },
      handleDelete({ $index, row }) {
        getRedPacketUrl(row.id).then(res => {
          window.open(res.data.url)
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .app-container {
    .roles-table {
      margin-top: 30px;
    }

    .permission-tree {
      margin-bottom: 30px;
    }
  }
</style>
